/**
 * Proto source:
 * https://github.com/SocialSisterYi/bilibili-API-collect/blob/master/grpc_api/bilibili/tv/interfaces/dm/v1/dm.proto
 */

import { strip } from '../utils';
export function DanmakuEventToXML(input: Uint8Array, doc: Document) {
  const event = DanmukuEvent.decode(input);
  const i = doc.documentElement;
  for (const elem of event.elems) {
    const d = doc.createElement('d');
    d.setAttribute(
      'p',
      [
        (elem.progress || 0) / 1000,
        elem.mode,
        elem.fontsize,
        elem.color,
        elem.ctime,
        elem.pool || 0,
        elem.midHash,
        elem.idStr,
      ].join(','),
    );
    d.textContent = strip(elem.content || '');
    i?.appendChild(d);
  }
}

// Code generated by protoc-gen-ts_proto. DO NOT EDIT.
// versions:
//   protoc-gen-ts_proto  v2.7.7
//   protoc               v6.32.0
// source: dm.proto

/* eslint-disable */
import { BinaryReader, BinaryWriter } from '@bufbuild/protobuf/wire';

/** 实时弹幕事件 */
export interface DanmukuEvent {
  /** 弹幕列表 */
  elems: DanmakuElem[];
}

/** 弹幕条目 */
export interface DanmakuElem {
  /** 弹幕dmid */
  id: string;
  /** 弹幕出现位置(单位为ms) */
  progress: number;
  /** 弹幕类型 */
  mode: number;
  /** 弹幕字号 */
  fontsize: number;
  /** 弹幕颜色 */
  color: number;
  /** 发送着mid hash */
  midHash: string;
  /** 弹幕正文 */
  content: string;
  /** 发送时间 */
  ctime: string;
  /** 弹幕动作 */
  action: string;
  /** 弹幕池 */
  pool: number;
  /** 弹幕id str */
  idStr: string;
}

function createBaseDanmukuEvent(): DanmukuEvent {
  return { elems: [] };
}

export const DanmukuEvent: MessageFns<DanmukuEvent> = {
  encode(
    message: DanmukuEvent,
    writer: BinaryWriter = new BinaryWriter(),
  ): BinaryWriter {
    for (const v of message.elems) {
      DanmakuElem.encode(v!, writer.uint32(10).fork()).join();
    }
    return writer;
  },

  decode(input: BinaryReader | Uint8Array, length?: number): DanmukuEvent {
    const reader =
      input instanceof BinaryReader ? input : new BinaryReader(input);
    const end = length === undefined ? reader.len : reader.pos + length;
    const message = createBaseDanmukuEvent();
    while (reader.pos < end) {
      const tag = reader.uint32();
      switch (tag >>> 3) {
        case 1: {
          if (tag !== 10) {
            break;
          }

          message.elems.push(DanmakuElem.decode(reader, reader.uint32()));
          continue;
        }
      }
      if ((tag & 7) === 4 || tag === 0) {
        break;
      }
      reader.skip(tag & 7);
    }
    return message;
  },

  create(base?: DeepPartial<DanmukuEvent>): DanmukuEvent {
    return DanmukuEvent.fromPartial(base ?? {});
  },
  fromPartial(object: DeepPartial<DanmukuEvent>): DanmukuEvent {
    const message = createBaseDanmukuEvent();
    message.elems = object.elems?.map((e) => DanmakuElem.fromPartial(e)) || [];
    return message;
  },
};

function createBaseDanmakuElem(): DanmakuElem {
  return {
    id: '0',
    progress: 0,
    mode: 0,
    fontsize: 0,
    color: 0,
    midHash: '',
    content: '',
    ctime: '0',
    action: '',
    pool: 0,
    idStr: '',
  };
}

export const DanmakuElem: MessageFns<DanmakuElem> = {
  encode(
    message: DanmakuElem,
    writer: BinaryWriter = new BinaryWriter(),
  ): BinaryWriter {
    if (message.id !== '0') {
      writer.uint32(8).int64(message.id);
    }
    if (message.progress !== 0) {
      writer.uint32(16).int32(message.progress);
    }
    if (message.mode !== 0) {
      writer.uint32(24).int32(message.mode);
    }
    if (message.fontsize !== 0) {
      writer.uint32(32).int32(message.fontsize);
    }
    if (message.color !== 0) {
      writer.uint32(40).uint32(message.color);
    }
    if (message.midHash !== '') {
      writer.uint32(50).string(message.midHash);
    }
    if (message.content !== '') {
      writer.uint32(58).string(message.content);
    }
    if (message.ctime !== '0') {
      writer.uint32(64).int64(message.ctime);
    }
    if (message.action !== '') {
      writer.uint32(74).string(message.action);
    }
    if (message.pool !== 0) {
      writer.uint32(80).int32(message.pool);
    }
    if (message.idStr !== '') {
      writer.uint32(90).string(message.idStr);
    }
    return writer;
  },

  decode(input: BinaryReader | Uint8Array, length?: number): DanmakuElem {
    const reader =
      input instanceof BinaryReader ? input : new BinaryReader(input);
    const end = length === undefined ? reader.len : reader.pos + length;
    const message = createBaseDanmakuElem();
    while (reader.pos < end) {
      const tag = reader.uint32();
      switch (tag >>> 3) {
        case 1: {
          if (tag !== 8) {
            break;
          }

          message.id = reader.int64().toString();
          continue;
        }
        case 2: {
          if (tag !== 16) {
            break;
          }

          message.progress = reader.int32();
          continue;
        }
        case 3: {
          if (tag !== 24) {
            break;
          }

          message.mode = reader.int32();
          continue;
        }
        case 4: {
          if (tag !== 32) {
            break;
          }

          message.fontsize = reader.int32();
          continue;
        }
        case 5: {
          if (tag !== 40) {
            break;
          }

          message.color = reader.uint32();
          continue;
        }
        case 6: {
          if (tag !== 50) {
            break;
          }

          message.midHash = reader.string();
          continue;
        }
        case 7: {
          if (tag !== 58) {
            break;
          }

          message.content = reader.string();
          continue;
        }
        case 8: {
          if (tag !== 64) {
            break;
          }

          message.ctime = reader.int64().toString();
          continue;
        }
        case 9: {
          if (tag !== 74) {
            break;
          }

          message.action = reader.string();
          continue;
        }
        case 10: {
          if (tag !== 80) {
            break;
          }

          message.pool = reader.int32();
          continue;
        }
        case 11: {
          if (tag !== 90) {
            break;
          }

          message.idStr = reader.string();
          continue;
        }
      }
      if ((tag & 7) === 4 || tag === 0) {
        break;
      }
      reader.skip(tag & 7);
    }
    return message;
  },

  create(base?: DeepPartial<DanmakuElem>): DanmakuElem {
    return DanmakuElem.fromPartial(base ?? {});
  },
  fromPartial(object: DeepPartial<DanmakuElem>): DanmakuElem {
    const message = createBaseDanmakuElem();
    message.id = object.id ?? '0';
    message.progress = object.progress ?? 0;
    message.mode = object.mode ?? 0;
    message.fontsize = object.fontsize ?? 0;
    message.color = object.color ?? 0;
    message.midHash = object.midHash ?? '';
    message.content = object.content ?? '';
    message.ctime = object.ctime ?? '0';
    message.action = object.action ?? '';
    message.pool = object.pool ?? 0;
    message.idStr = object.idStr ?? '';
    return message;
  },
};

type Builtin =
  | Date
  | Function
  | Uint8Array
  | string
  | number
  | boolean
  | undefined;

export type DeepPartial<T> = T extends Builtin
  ? T
  : T extends globalThis.Array<infer U>
    ? globalThis.Array<DeepPartial<U>>
    : T extends ReadonlyArray<infer U>
      ? ReadonlyArray<DeepPartial<U>>
      : T extends {}
        ? { [K in keyof T]?: DeepPartial<T[K]> }
        : Partial<T>;

export interface MessageFns<T> {
  encode(message: T, writer?: BinaryWriter): BinaryWriter;
  decode(input: BinaryReader | Uint8Array, length?: number): T;
  create(base?: DeepPartial<T>): T;
  fromPartial(object: DeepPartial<T>): T;
}
